<?php

namespace App\Controller\WeChat;

use App\Base\BaseController4Restful;
use App\Service\Dlog;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

const TOKEN = "LLLOVENN";
const AESKEY = "HmeC9YPUUP4vOhG0itiWWAlVX3z7AMhDqU0LAgzHtDP";
class WeChatApiController extends BaseController4Restful
{
    /**
     * @Route("/we/chat/we/chat/api", name="we_chat_we_chat_api")
     */
    public function we_chat_we_chat_api(Dlog $dlog)
    {
        if ($this->checkSignature()) {
            $data = [
                "post"=>$_POST,
                "get"=>$_GET,
                "req"=>$_REQUEST,
                "input"=>file_get_contents('php://input'),
            ];
            $dlog->add($data);
            if (!$data["input"]) {
                echo $_GET["echostr"];die();
            }else{
                $data = json_decode(file_get_contents('php://input'));
                $dlog->add($data);
                //todo 消息回復
            }
        }else{
            die("success");
        }
    }
    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];

        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if ($tmpStr == $signature ) {
            return true;
        } else {
            return false;
        }
    }
    /**
     * @Route("/we_chat_scan", name="we_chat_scan")
     */
    public function we_chat_scan(Dlog $dlog)
    {
        die("请使用微信扫码");
    }
}
